<template>
  <div>
    <div class="form-class">
      <el-card>
        <el-form :model="form" :rules="rules" ref="form" label-width="70px">
          <div v-if="!forgetPwd">
            <el-form-item  label="账号" prop="username">
              <el-input
                v-model="form.username"
              />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="form.password"
                type="password"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit" :loading="load" style="width: 100%;">{{ loginText }}</el-button>
              <el-button type="text" @click="forget">忘记密码？</el-button>
            </el-form-item>
          </div>
          <div v-else="">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入忘记密码账号的邮箱"/>
            </el-form-item>
            <el-button type="primary" @click="findPassword" style="width: 100%;">找回密码</el-button>
          </div>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: 'login',
  data () {
    return {
      // 表单对象
      form: {},
      // 表单效验规则
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { required: true, message: '请输入正确的邮箱地址', trigger: ['blur', 'change']}
        ],
      },
      // 登录按钮的文字
      loginText:'登录',
      // 登录状态
      load: false,
      forgetPwd: false,
    }
  },
  // 页面加载
  created () {

  },
  // 存放所有的方法
  methods: {
    // 登录
    submit () {
      this.$refs.form.validate((valid) => {
        if(!valid) return this.$message.error("数据校验失败，请检查后提交");
        this.load = true;
        this.loginText = '登录中...';
        //如果数据校验成功，则向后端发送请求进行登录
        this.$ajax.post('/user/login',this.form).then((res) => {
          const tokenBody = res.data.data;
          let tokenHead = tokenBody.tokenHead;
          let token = tokenBody.token;
          this.$store.commit('setToken',tokenHead + token);
          this.$router.push('/');
        }).catch(() => {
          this.load = false;
          this.loginText = '登录';
        });
      });
    },
    /** 忘记密码 **/
    forget() {
      this.forgetPwd = true;
    },
    /** 找回密码 **/
    findPassword() {
      this.$refs.form.validate((valid) => {
        if(!valid) return this.$message.error("数据校验失败，请检查后提交");
        //如果数据校验成功，则向后端发送请求进行登录
        this.$ajax.post('/Tool/forget/password', { receivers: [this.form.email] }).then((res) => {
          if (res.data.flag) {
            this.$message.success(res.data.message);
            this.forgetPwd = false;
          }
        });
      });
    }
  }
}
</script>

<style scoped>
.form-class{

  width: 30%;
  margin: 200px 500px auto;
}
</style>
